package com.itheima.before_leyou_store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.before_leyou_store.mapper.SkuMapper;
import com.itheima.before_leyou_store.pojo.Sku;
import com.itheima.before_leyou_store.service.SkuService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService  {
    @Override
    public int findTotal() {
       return baseMapper.findTotal();
    }

    @Override
    public List<Sku> findSkus(int i) {
        return baseMapper.findSkus(i);
    }

    @Override
    public Sku test() {
        return baseMapper.test();
    }

    @Override
    public Sku findById(String id) {
       RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.187.128", 9200, "http")));
        GetRequest request = new GetRequest("sku",id);

        GetResponse res = null;
        try {
            res = client.get(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> source = res.getSource();
        Sku sku = JSON.parseObject(JSON.toJSONString(source), Sku.class);
        return sku;


    }
}
